package com.krm;

//import com.sun.tools.javac.util.StringUtils;

import java.util.List;

public abstract class ToSql {


    /**
     * 转成 sql create table
     * @param exceBeanList
     * @return
     */
    public abstract String toSql(List<ExcelBean> exceBeanList);

    /**
     * 转成sql inster
     * @param sqlInsterList
     * @return
     */
    public String toSqlInster(List<SQLInsterBean> sqlInsterList){
        StringBuilder sb = new StringBuilder();

        if(sqlInsterList==null||sqlInsterList.size()<=0){
            return "Excel有问题未能解析出数据";
        }
        StringBuilder head = new StringBuilder();
        for(SQLInsterBean si : sqlInsterList){
            if(si==null){
                continue;
            }
            head.append(" INSERT INTO ");
            head.append(" "+si.getSheetName()+" ( ");
            //字段
            List<String> headList =  si.getHeadlist();
            for(int i=0;i<headList.size();i++){

                head.append(headList.get(i));
                if(i<(headList.size()-1)){
                    head.append(",");
                }
            }
            head.append(" ) VALUES ( ");
            //数据
            List<List<Object>> dataList =   si.getDatalist();
            for(List<Object> datas : dataList){
                if(datas==null||datas.size()==0){
                    continue;
                }
                StringBuilder data = new StringBuilder();
                for(int j=0;j<datas.size();j++){
                    Object value = datas.get(j);
                    if(value==null){
                        data.append("''");
                    }
                    if(value instanceof String){
                        data.append("'"+value+"'");
                    }
                    if(value instanceof Integer){
                        data.append(value);
                    }
                    if(value instanceof Double){
                        data.append(value);
                    }
                    if(j<(datas.size()-1)){
                        data.append(",");
                    }
                }
                if(headList.size()!=datas.size()){
                    data.append(" ,'' ");
                }
                data.append(" );\r\n ");
                String sqlInster = head.toString()+ data.toString();
                System.out.println("---sqlInster----"+sqlInster);
                sb.append(sqlInster);
            }
        }
        sb.append("commit;");
        return sb.toString();
    }



}
